package com.anolesoft.yr.mbus.pre.service.impl;

import com.anolesoft.yr.mbus.pre.dto.PrePromoteDeleteDTO;
import com.anolesoft.yr.mbus.pre.dto.PrePromoteSaveDTO;
import com.anolesoft.yr.mbus.pre.dto.PrePromoteUpdateDTO;
import com.anolesoft.yr.mbus.pre.entity.PreInqutry;
import com.anolesoft.yr.mbus.pre.entity.PrePromote;
import com.anolesoft.yr.mbus.pre.mapper.PrePromoteMapper;
import com.anolesoft.yr.mbus.pre.service.PrePromoteService;
import com.anolesoft.yr.mbus.pre.vo.PrePromoteQueryVO;
import com.anolesoft.yr.mbus.pre.vo.PrePromoteQueryVO2;
import com.anolesoft.yr.mbus.util.GlobalLoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

@Service
public class PrePromoteServiceImpl implements PrePromoteService {

    private static final String CODE_EXIST = "CODE_EXIST";
    private static final String SUCCESS = "SUCCESS";
    private static final String EXCEPTION = "EXCEPTION";

    @Autowired
    PrePromoteMapper prePromoteMapper;

    //新增推广记录
    @Override
    @Transactional
    public String savePrePromote(PrePromoteSaveDTO prePromoteSaveDTO) throws Exception {
        try {
            int count = 0;

            count = prePromoteMapper.queryPromoteByCode(prePromoteSaveDTO.getPromoteCode());
            if (count > 0) {
                return CODE_EXIST;
            }

            int rows = 0;
            rows = prePromoteMapper.savePrePromote(prePromoteSaveDTO);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //修改推广记录
    @Override
    @Transactional
    public String updatePrePromote(PrePromoteUpdateDTO prePromoteUpdateDTO) throws Exception {
        try {
            int count = 0;

            PrePromoteQueryVO2 prePromoteQueryVO2 = prePromoteMapper.queryPrePromoteByUid(prePromoteUpdateDTO.getObjuid());
            count = prePromoteMapper.queryPromoteByCode2(prePromoteUpdateDTO.getPromoteCode(), prePromoteQueryVO2.getUpdatePromoteCode());
            if (count > 0) {
                return CODE_EXIST;
            }

            int rows = 0;
            rows = prePromoteMapper.updatePrePromote(prePromoteUpdateDTO);
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //删除推广记录
    @Override
    @Transactional
    public String deletePrePromote(List<PrePromoteDeleteDTO> prePromoteDeleteDTOList) throws Exception {
        try {
            int rows = 0;
            for (int i = 0; i < prePromoteDeleteDTOList.size(); i++) {
                PrePromoteDeleteDTO prePromoteDeleteDTO = prePromoteDeleteDTOList.get(i);
                rows += prePromoteMapper.deletePrePromote(prePromoteDeleteDTO);
            }

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //提交推广记录
    @Override
    @Transactional
    public String submitPrePromote(List<PrePromoteDeleteDTO> prePromoteDeleteDTOList) throws Exception {
        try {
            int rows = 0;
            for (int i = 0; i < prePromoteDeleteDTOList.size(); i++) {
                PrePromoteDeleteDTO prePromoteDeleteDTO = prePromoteDeleteDTOList.get(i);
                rows += prePromoteMapper.submitPrePromote(prePromoteDeleteDTO);
            }

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //根据主键查询服务项基础唯一数据
    @Override
    public Map<String, Object> queryPrePromoteByUid(String uid) throws Exception {
        PrePromoteQueryVO2 prePromoteQueryVO2 = prePromoteMapper.queryPrePromoteByUid(uid);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("prePromoteQueryVO2", prePromoteQueryVO2);
        return maps;
    }

    //根据条件查询推广记录所有数据
    @Override
    public Map<String, Object> queryPrePromoteAll(String infoSource, String sourceTime, String intoCompany, int page, int limit) throws Exception {
        int count = prePromoteMapper.queryPrePromoteAllCount(infoSource, sourceTime, intoCompany);
        List<PrePromoteQueryVO> prePromoteVOList = prePromoteMapper.queryPrePromoteAll(infoSource, sourceTime, intoCompany, (page - 1) * limit, limit);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("count", count);
        maps.put("prePromoteVOList", prePromoteVOList);
        return maps;
    }

    //根据条件查询推广记录所有数据
    @Override
    public Map<String, Object> queryPrePromoteAll2(String infoSource, String sourceTime, String intoCompany, String state, int page, int limit) throws Exception {
        int count = prePromoteMapper.queryPrePromoteAllCount2(infoSource, sourceTime, intoCompany, state);
        List<PrePromoteQueryVO> prePromoteVOList = prePromoteMapper.queryPrePromoteAll2(infoSource, sourceTime, intoCompany, state, (page - 1) * limit, limit);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("count", count);
        maps.put("prePromoteVOList", prePromoteVOList);
        return maps;
    }

    //根据条件查询推广记录所有数据
    @Override
    public Map<String, Object> queryPrePromoteAll3(String infoSource, String sourceTime, String intoCompany, int page, int limit) throws Exception {
        int count = prePromoteMapper.queryPrePromoteAllCount3(infoSource, sourceTime, intoCompany);
        List<PrePromoteQueryVO> prePromoteVOList = prePromoteMapper.queryPrePromoteAll3(infoSource, sourceTime, intoCompany, (page - 1) * limit, limit);

        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("count", count);
        maps.put("prePromoteVOList", prePromoteVOList);
        return maps;
    }

    //查询地区信息（临时查询，正常应该在地区接口中）
    @Override
    public List<PrePromoteQueryVO> getRegionName() throws Exception {
        List<PrePromoteQueryVO> prePromoteQueryVOList = prePromoteMapper.getRegionName();
        return prePromoteQueryVOList;
    }

    //查询人员信息（临时查询，正常应该在人员接口中）
    @Override
    public List<PrePromoteQueryVO> queryFacEmployeeName() throws Exception {
        List<PrePromoteQueryVO> prePromoteQueryVOList = prePromoteMapper.queryFacEmployeeNameAll();
        return prePromoteQueryVOList;
    }

    //派工推广记录
    @Override
    @Transactional
    public String assignPrePromote(PrePromote prePromote) throws Exception {
        try {
            int rows = 0;
            List<PreInqutry> preInqutryList = prePromote.getPreInqutry();
            for (int i = 0; i < preInqutryList.size(); i++) {
                PreInqutry preInqutry = preInqutryList.get(i);

                PrePromoteUpdateDTO prePromoteUpdateDTO = new PrePromoteUpdateDTO();
                prePromoteUpdateDTO.setState(2);
                prePromoteUpdateDTO.setSalesManager(GlobalLoginInfo.getGlobalLoginUser());
                prePromoteUpdateDTO.setDispatchTime(GlobalLoginInfo.getGlobalLoginTime());
                prePromoteUpdateDTO.setSalesPerson(prePromote.getSalesPerson());
                prePromoteUpdateDTO.setObjuid(preInqutry.getObjuid());

                rows += prePromoteMapper.assignPrePromote(prePromoteUpdateDTO);
            }

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw new TimeoutException(EXCEPTION);
        }
    }

    //查询客户名称信息（临时查询，正常应该在客户接口中）
    @Override
    public List<PrePromoteQueryVO> queryCrmBaseName() throws Exception {
        List<PrePromoteQueryVO> prePromoteQueryVOList = prePromoteMapper.queryCrmBaseName();
        return prePromoteQueryVOList;
    }

    //查询客户联系人、联系方式信息（临时查询，正常应该在客户接口中）
    @Override
    public PrePromoteQueryVO queryCrmBase2(String objuid) throws Exception {
        PrePromoteQueryVO prePromoteQueryVO = prePromoteMapper.queryCrmBase2(objuid);
        return prePromoteQueryVO;
    }

    //根据主键查询推广记录，用于询盘回访界面的显示
    @Override
    public PrePromoteQueryVO queryPrePromoteByUid2(String objuid) throws Exception {
        PrePromoteQueryVO prePromoteQueryVO = prePromoteMapper.queryPrePromoteByUid2(objuid);
        return prePromoteQueryVO;
    }

}
